package cn.exrick.xboot.modules.warning.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.warning.entity.Warning;
import cn.exrick.xboot.modules.warning.service.IWarningService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "预警接口管理接口")
@RequestMapping("/xboot/warning")
@Transactional
public class WarningController {

    @Autowired
    private IWarningService iWarningService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Warning> get(@PathVariable String id){

        Warning warning = iWarningService.getById(id);
        return new ResultUtil<Warning>().setData(warning);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Warning>> getAll(){

        List<Warning> list = iWarningService.list();
        return new ResultUtil<List<Warning>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Warning>> getByPage(@ModelAttribute PageVo page){

        IPage<Warning> data = iWarningService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Warning>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Warning> saveOrUpdate(@ModelAttribute Warning warning){
        warning.setUpdateTime(new Date());
        if(iWarningService.saveOrUpdate(warning)){
            return new ResultUtil<Warning>().setData(warning);
        }
        return new ResultUtil<Warning>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iWarningService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
}
